package com.hx.mylombok.task.impl;

import com.hx.mylombok.task.AbstractFileTask;
import com.hx.mylombok.task.FileTask;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
/**
 * @author guoxiaolin
 * @description
 * @since 2019/3/27 15:10
 */
public class ControllerTask  extends AbstractFileTask implements FileTask {

    public ControllerTask(String className) {
        super(className);
    }

    @Override
    public void generateFile(Map<String, String> setterMap, ProcessingEnvironment processingEnv) throws IOException {

        JavaFileObject builderControllerFile = processingEnv.getFiler().
                createSourceFile(packageName+".controller.ControllerService"+sufixName);

        try (PrintWriter out = new PrintWriter(builderControllerFile.openWriter())) {

            if (packageName != null) {
                out.print("package ");
                out.print(packageName);
                out.println(".controller;");
                out.println();
            }
            out.println("import com.hx.battery.common.hibernate.controller.AbstractController;");
            out.println("import com.hx.battery.common.hibernate.service.IService;");
            out.println("import "+packageName+".domain."+sufixName+";");
            out.println("import "+packageName+".service."+simpleClassName+";");
            out.println("import io.swagger.annotations.Api;");
            out.println("import org.springframework.beans.factory.annotation.Autowired;");
            out.println("import org.springframework.web.bind.annotation.*;");
            out.println();
            out.println("@Api(description = \"xxxx\")");
            out.println("@RestController");
            out.println("@RequestMapping(\"/"+sufixName+"\")");
            out.println("public class ControllerService" + sufixName + " extends AbstractController<"+sufixName+"> {");
            out.println();
            out.println("    private "+simpleClassName+" service"+sufixName+";");
            out.println("    public ControllerService"+sufixName+"(){}");
            out.println("    @Autowired");
            out.println("    public ControllerService"+sufixName+"( "+simpleClassName+" service"+sufixName+"){");
            out.println("        this.service"+sufixName+" = service"+sufixName+";");
            out.println("    }");
            out.println("    @Override");
            out.println("    public IService<"+sufixName+"> getServcie() {");
            out.println("        return service"+sufixName+";");
            out.println("    }");
            out.println();
            out.println("}");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            logger.info("ControllerTask of " + simpleClassName + " is over");
        }
    }
}
